package com.edou.cms.controller.admin;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.json.simple.JSONObject;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.edou.cms.form.CmsColumnForm;
import com.edou.cms.entity.CmsColumn;
import com.edou.cms.entity.CmsColumnTag;
import com.edou.cms.entity.CmsTag;
import com.edou.cms.service.ICmsColumnService;
import com.edou.cms.service.ICmsColumnTagService;
import com.edou.cms.service.ICmsTagService;
import com.edou.base.token.TokenValidate;
import com.edou.base.controller.BaseController;


@Controller("cmsColumnController")
@Scope("prototype")
@RequestMapping("/admin/cms/column")
public class CmsColumnController extends BaseController {

	@Resource(name = "cmsColumnServiceImpl")
	private ICmsColumnService cmsColumnService;
	@Resource(name = "cmsTagServiceImpl")
	private ICmsTagService cmsTagService;
	@Resource(name = "cmsColumnTagServiceImpl")
	private ICmsColumnTagService cmsColumnTagService;


	/**
	 * 首页
	 * @return 
	 */
	@RequestMapping(value = "/index.htm", method = RequestMethod.GET)
	public String index(){
		return "/admin/cms/cms_column_index";
	}

	/**
	 * 分页查询
	 * @return 
	 */
	@RequestMapping(value = "/listCmsColumnByPage.htm", method = RequestMethod.POST)
	public void listCmsColumnByPage(@ModelAttribute CmsColumnForm model,HttpServletRequest request,HttpServletResponse response,HttpSession session){
		try{
			cmsColumnService.listCmsColumnByPage(model.getPageResult(),model.getCondition());
			JSONObject root = toPageJson(model.getPageResult(), new String[]{"id","name","descBrief","descHtml","sort","numArticle","countCollect","countView","countLike","isBest","isRecommend","isNew"});
			ajaxPageResult(root,response);
		}catch(Exception ex){
			String msg = "查询专栏管理时发生异常："+ex.getMessage();
			logger.error(msg,ex);
		}
	}

	/**
	 * 保存专栏管理
	 * @return 
	 */
	@RequestMapping(value = "/edit.htm", method = RequestMethod.GET)
	public ModelAndView edit(@ModelAttribute CmsColumnForm model,HttpServletRequest request,HttpServletResponse response,HttpSession session){
		ModelAndView view = new ModelAndView();
		view.setViewName("/admin/cms/cms_column_add");
		Map<String,Object> data = view.getModel();
		if(StringUtils.isNotBlank(model.getItem().getId())){
			CmsColumn item = cmsColumnService.getCmsColumnById(model.getItem().getId());
			List<CmsColumnTag> columnTag = cmsColumnTagService.listColumnTagByColumnId(model.getItem().getId());
			String tagList = "";
			for (CmsColumnTag cmsColumnTag : columnTag) {
				if(StringUtils.isNotBlank(tagList)){
					tagList += ","+cmsColumnTag.getTagId();
				}else{
					tagList = cmsColumnTag.getTagId();
				}
			}
			item.setTagList(tagList);
			data.put("item", item);
		}
		//查询标签
		List<CmsTag> listCmsTagAll = cmsTagService.listCmsTagAll();
		data.put("cmsTagList", listCmsTagAll);
		
		return view;
	}

	/**
	 * 保存专栏管理
	 * @return 
	 */
	@RequestMapping(value = "/saveCmsColumn.htm", method = RequestMethod.POST)
	@TokenValidate
	public ModelAndView saveRole(@ModelAttribute CmsColumnForm model,HttpServletRequest request,HttpSession session){
		try {
			if(StringUtils.isBlank(model.getItem().getId())){
				cmsColumnService.add(model.getItem());
			}else{
				cmsColumnService.update(model.getItem());
			}
			return ajaxJSON(Status.success,model.getItem().getId());
		} catch (Exception e) {
			String msg = "保存专栏管理时发生异常：" + e.getMessage();
			logger.error(msg,e);
			return ajaxJSON(Status.error, "保存失败，请稍后重试");
		}
	}
	/**
	 * 删除CmsColumn
	 * @return 
	 */
	@RequestMapping(value = "/del.htm", method = RequestMethod.POST)
	public ModelAndView del(@ModelAttribute CmsColumnForm model,HttpServletRequest request,HttpServletResponse response,HttpSession session){
		try {
			cmsColumnService.delByIds(model.getSelIds().split(","));
			return ajaxJSON(Status.success,"删除成功");
		} catch (Exception e) {
			String msg = "删除专栏管理时发生异常：" + e.getMessage();
			logger.error(msg,e);
			return ajaxJSON(Status.error, "删除失败，请稍后重试");
		}
	}



}

